package org.ilead.iserve.web;

import java.io.File;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.ilead.iserve.common.CookiesHelper;
import org.ilead.iserve.constants.CookiesKeyConstant;
import org.ilead.iserve.constants.LopConstants;
import org.ilead.iserve.constants.SessionKeyConstant;
import org.ilead.iserve.model.User;
import org.ilead.iserve.service.IUserService;
import org.ilead.iserve.service.manager.AreaManager;
import org.ilead.iserve.service.manager.HomePageManager;
import org.ilead.iserve.service.manager.UserManager;
import org.ilead.iserve.util.FileUpLoadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 个人主页Action
 * @author jill
 *
 */
public class HomePageAction extends ActionSupport {
	private static Logger logger = LoggerFactory.getLogger(HomePageAction.class);
	private IUserService userService = null;
	private User u;
	private String password;
	
	private File doc;
	private String fileName;

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public String homePage(){
		Map<String, Object> sessionMap = ActionContext.getContext().getSession();
		sessionMap.put(SessionKeyConstant.AREAS, AreaManager.getInstance().getAll());
		return SUCCESS;
	}
	
	public String updatePasswordPage(){
		return SUCCESS;
	}
	
	public String updatePassword(){
		User user = UserManager.getInstance().getLoginUser();
		ActionContext context = ActionContext.getContext();
		String[] passwordArray = (String[])context.getParameters().get("password");
		user.setPassword(passwordArray[0]);
		try {
			userService.updateUser(user);
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			if(CookiesHelper.hasCookie(request, CookiesKeyConstant.LOP_AUOT_LOGIN_FLAG)){
				HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
				CookiesHelper.setCookie(request, response,CookiesKeyConstant.LOP_LOGIN_PASSWORD, user.getPassword());
			}
		} catch (Exception e) {
			logger.error("更新密码错误", e);
			return Action.ERROR;
		}
		return Action.SUCCESS;
	}
	
	public String updateUserInfo(){
		try {
			User user = UserManager.getInstance().getLoginUser();
			HomePageManager.getInstance().saveHomePage(user.getId(), user.getHomePageUrl(), user.getSignature());
			userService.updateUser(user);
		} catch (Exception e) {
			logger.error("更新用户信息错误", e);
			return Action.ERROR;
		}
		return Action.SUCCESS;
	}
	
	public String updateAvatar(){
		try {
			if(doc == null)
				return null;
			
			User user = UserManager.getInstance().getLoginUser();
			String imageFileName = new Date().getTime() + FileUpLoadUtil.getExtention(fileName);
			File imageFile = new File(ServletActionContext.getServletContext()
					.getRealPath(LopConstants.UPLOAD_AVATAR_PATH) + File.separatorChar + imageFileName);
			FileUpLoadUtil.copy(doc, imageFile);
			String oldImage = user.getImage();
			if(oldImage != null && !"".equals(oldImage)){
				String pathName = ServletActionContext.getServletContext().getRealPath("/");
				String oldImagePath = pathName + LopConstants.UPLOAD_AVATAR_PATH + File.separatorChar + oldImage;
				new File(oldImagePath).delete();
			}
				
			user.setImage(imageFileName);
			userService.updateUser(user);
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			ActionContext.getContext().getSession().put(SessionKeyConstant.AVATAR_URL, UserManager.getInstance().getAvatarUrl(request, user));
			return SUCCESS;
		} catch (Exception e) {
			logger.error("", e);
			return ERROR;
		}
	}
	
	public void setPassword(String password) {
		this.password = password;
	}

	public User getU() {
		return UserManager.getInstance().getLoginUser();
	}

	public void setU(User u) {
		this.u = u;
	}
	
}
